package com.ysc.task.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysc.task.entity.Task;
import com.ysc.task.entity.dto.TaskAddDTO;
import com.ysc.task.service.ITaskService;
import com.ysc.task.task.DynamicTaskManager;
import com.ysc.task.util.EmailTool;
import com.ysc.task.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * Version: 1.0
 * Class Name: TaskController
 * Description: 定时任务Controller
 * Author: ysc
 * Date: 2024/3/31 10:59
 */

@RestController
@RequestMapping("/task")
@RequiredArgsConstructor
@CrossOrigin
public class TaskController {

    private final DynamicTaskManager dynamicTaskManager;
    private final ITaskService taskService;
    private final EmailTool emailTool;

    @PostMapping("/test")
    public String testTask(@RequestParam("cron") String cron,
                           @RequestParam("data") String data,
                           @RequestParam("id") String id) {
        dynamicTaskManager.addTask(id, () -> {
            System.out.println("测试添加" + data);
            emailTool.sendEmail("2725843507@qq.com", "验证邮箱测试",
                    "这只是测试的内容");
            dynamicTaskManager.removeTask(id);
        }, cron);
        return "添加成功";
    }

    @DeleteMapping("/delete")
    public String deleteTask(@RequestParam("id") String id) {
        dynamicTaskManager.removeTask(id);
        return "删除成功";
    }

    @PostMapping("/add")
    public R add(@RequestBody TaskAddDTO taskAddDTO) {
        return taskService.add(taskAddDTO);
    }

    @GetMapping("/remove/{id}")
    public R remove(@PathVariable Long id) {
        return taskService.delete(id);
    }

    @GetMapping("/enable/{id}")
    public R enable(@PathVariable Long id) {
        return taskService.enable(id);
    }

    @GetMapping("/unEnable/{id}")
    public R unEnable(@PathVariable Long id) {
        return taskService.unEnable(id);
    }

    @GetMapping("/list/{id}")
    public R getList(@PathVariable Long id) {
        return taskService.getList(id);
    }

    @GetMapping("/getOne/{id}")
    public R getOne(@PathVariable Long id) {
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskId, id));
        return R.ok().data("task", task);
    }

}
